//
//  LoginViewController.m
//  Drops
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "LoginViewController.h"
#import "ZFK_squareMainViewController.h"
#import "rigestViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <BmobSDK/Bmob.h>
#import "ZFK_buttonView.h"
#define WWWW [UIScreen mainScreen].bounds.size.width
#define HHHH [UIScreen mainScreen].bounds.size.height
@interface LoginViewController () <UITextFieldDelegate>
@property(nonatomic, strong)AVPlayer *player;
@property(nonatomic, strong)UITextField *IdText;
@property(nonatomic, strong)UITextField *passwordText;
@property(nonatomic, strong)NSMutableArray *array;
@property(nonatomic, assign)BOOL isExist;
@end

@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated{
    
    [self.player play];                                                                                                                                                             }
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.isExist = NO;
    
    [self setupForAVplayerView];
    UIVisualEffect *blurEffect;
    blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *visualEffectView;
    visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5f];
    [visualEffectView setAlpha:0.4f];
    [UIView commitAnimations];
    [visualEffectView setFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:visualEffectView];
    
    UIView *firstLine = [[UIView alloc]initWithFrame:CGRectMake(WWWW / 8, 150 , WWWW * 3 / 4 , 1)];
    firstLine.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:firstLine];
    UILabel *IdLabel = [[UILabel alloc]initWithFrame:CGRectMake(WWWW / 8, 120, 50, 30)];
    IdLabel.text = @"账号:";
    IdLabel.font = [UIFont systemFontOfSize:18];
    IdLabel.textColor = [UIColor whiteColor];
    IdLabel.textAlignment = 1;
    IdLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:IdLabel];
    _IdText = [[UITextField alloc]initWithFrame:CGRectMake(WWWW / 8 + 50, 120, (WWWW * 3 / 4) - (WWWW / 8), 30)];
    _IdText.backgroundColor = [UIColor clearColor];
    _IdText.placeholder = @"请输入账号";
    _IdText.textAlignment = 1;
    _IdText.font = [UIFont systemFontOfSize:20];
    _IdText.delegate = self;
    [self.view addSubview:_IdText];
    
    UIView *secLine = [[UIView alloc]initWithFrame:CGRectMake(WWWW / 8, 200 , WWWW * 3 / 4 , 1)];
    secLine.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:secLine];
    UILabel *passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(WWWW / 8, 170, 50, 30)];
    passwordLabel.text = @"密码:";
    passwordLabel.font = [UIFont systemFontOfSize:18];
    passwordLabel.textColor = [UIColor whiteColor];
    passwordLabel.textAlignment = 1;
    passwordLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:passwordLabel];
    _passwordText = [[UITextField alloc]initWithFrame:CGRectMake(WWWW / 8 + 50, 170, (WWWW * 3 / 4) - (WWWW / 8), 30)];
    _passwordText.backgroundColor = [UIColor clearColor];
    _passwordText.placeholder = @"请输入密码";
    _passwordText.textAlignment = 1;
    _passwordText.secureTextEntry = YES;
    _passwordText.clearButtonMode = YES;
    _passwordText.font = [UIFont systemFontOfSize:20];
    _passwordText.delegate = self;
    [self.view addSubview:_passwordText];
    
    ZFK_buttonView *buttonView = [[ZFK_buttonView alloc] initWithFrame:CGRectMake(0, sHEIGHT / 2, sWIDTH, sHEIGHT / 2)];
    [self.view addSubview:buttonView];
    [buttonView.loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView.registBtn addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView.backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)setupForAVplayerView
{
    //创建播放图层
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame =[UIScreen mainScreen].bounds;
    [self.view.layer addSublayer:playerLayer];
}
- (AVPlayer *)player
{
    if (!_player) {
        AVPlayerItem *playerItem = [self getPlayItem];
        _player = [AVPlayer playerWithPlayerItem:playerItem];
        //设置重复播放
        _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        
        [[NSNotificationCenter defaultCenter]addObserver:self
                                                selector:@selector(__playerItemDidPlayToEndTimeNotification:)
                                                    name:AVPlayerItemDidPlayToEndTimeNotification
                                                  object:nil];
        
    }
    return _player;
}
- (void)__playerItemDidPlayToEndTimeNotification:(NSNotification *)sender
{
    [_player seekToTime:kCMTimeZero];
}

- (AVPlayerItem *)getPlayItem
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"BridgeLoop-640p" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    return playerItem;
}
//- (IBAction)login:(id)sender {
//    BmobQuery *bquery = [BmobQuery queryWithClassName:@"users"];
//    bquery.limit = 1000;
//    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
//        for (BmobObject *obj in array) {
//            NSString *str = [obj objectForKey:@"userID"];
//            NSString *string = [obj objectForKey:@"userPassword"];
//            if ([self.IdText.text isEqualToString:str] && [self.passwordText.text isEqualToString:string]) {
//                self.isExist = YES;
//                break;
//            }
//        }
//        if (self.isExist == YES) {
//           [self dismissViewControllerAnimated:YES completion:^{
//               
//               
//           }];
//            self.isExist = NO;
//        }else{
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号信息有误" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:nil, nil];
//            [alert show];
//        }
//    }];
//
//}

- (void)loginAction:(UIButton *)button {
    self.array = [NSMutableArray array];
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"users"];
    bquery.limit = 1000;
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            NSString *str = [obj objectForKey:@"userID"];
            NSString *string = [obj objectForKey:@"userPassword"];
            if ([self.IdText.text isEqualToString:str] && [self.passwordText.text isEqualToString:string]) {
                [self.array addObject:self.IdText.text];
                BmobFile *file = [obj objectForKey:@"headhead"];
                self.str = file.url;
                if (self.str != nil) {
                    [self.array addObject:self.str];
                }
                self.isExist = YES;
                break;
            }
        }
        if (self.isExist == YES) {
            [self dismissViewControllerAnimated:YES completion:^{
                [self.delegate sendValue:self.array];
            }];
            
            self.isExist = NO;
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号信息有误" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

//- (IBAction)rigest:(id)sender {
//    rigestViewController *vc = [[rigestViewController alloc]init];
//    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    [self presentViewController:vc animated:YES completion:^{
//        
//    }];
//
//}

- (void)registAction:(UIButton *)button {
    rigestViewController *vc = [[rigestViewController alloc]init];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

//- (IBAction)return:(id)sender {
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
//}

- (void)backAction:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    self.IdText.text = @"";
    self.passwordText.text = @"";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
